package com.analysis.service;

import com.analysis.entity.BrandExposureSnapshot;
import com.analysis.entity.BrandExposureSnapshotDetail;
import com.analysis.mapper.BrandExposureSnapshotDetailMapper;
import com.analysis.mapper.BrandExposureSnapshotMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class BrandExposureSnapshotService {

    @Autowired
    private BrandExposureSnapshotMapper snapshotMapper;

    @Autowired
    private BrandExposureSnapshotDetailMapper snapshotDetailMapper;

    @Transactional
    public int create(BrandExposureSnapshot snapshot) {

        // 1. 先保存快照主表
        int result = snapshotMapper.insert(snapshot);

        // 2. 如果传了content，保存到详情表
        if (result > 0 && StringUtils.isNotBlank(snapshot.getContent())) {
            BrandExposureSnapshotDetail detail = new BrandExposureSnapshotDetail();
            detail.setProjectId(snapshot.getProjectId());
            detail.setCompanyId(snapshot.getCompanyId());
            detail.setSnapshotId(snapshot.getId());
            detail.setContent(snapshot.getContent());
            snapshotDetailMapper.insert(detail);
        }
        return result;
    }

    @Transactional
    public int delete(Long id) {
        // 1. 先查询快照信息，获取projectId
        BrandExposureSnapshot snapshot = snapshotMapper.selectById(id);
        if (snapshot == null) {
            return 0; // 快照不存在
        }

        // 2. 删除快照详情
        snapshotDetailMapper.deleteByProjectIdAndSnapshotId(snapshot.getProjectId(), snapshot.getId());

        // 3. 删除快照主表
        return snapshotMapper.deleteById(id);
    }

    @Transactional
    public int update(BrandExposureSnapshot snapshot) {
        int size = snapshotMapper.update(snapshot);

        // 2. 如果传了content，保存到详情表
        if (size > 0 && StringUtils.isNotBlank(snapshot.getContent())) {
            BrandExposureSnapshotDetail existDetail = snapshotDetailMapper.getByProjectIdAndSnapshotId(snapshot.getProjectId(), snapshot.getId());
            if (existDetail != null) {
                existDetail.setContent(snapshot.getContent());
                snapshotDetailMapper.update(existDetail);
            } else {
                BrandExposureSnapshotDetail detail = new BrandExposureSnapshotDetail();
                detail.setProjectId(snapshot.getProjectId());
                detail.setCompanyId(snapshot.getCompanyId());
                detail.setSnapshotId(snapshot.getId());
                detail.setContent(snapshot.getContent());
                snapshotDetailMapper.insert(detail);
            }
        }
        return size;
    }

    public BrandExposureSnapshot getById(Long id) {
        return snapshotMapper.selectById(id);
    }

    public BrandExposureSnapshot getWithContentById(Long id) {
        BrandExposureSnapshot snapshot = snapshotMapper.selectById(id);
        if (snapshot != null) {
            snapshot.setContent(snapshotDetailMapper.selectByProjectIdAndSnapshotId(snapshot.getProjectId(), snapshot.getId()));
        }
        return snapshot;
    }

    public List<BrandExposureSnapshot> listByProject(Long projectId) {
        return snapshotMapper.selectByProjectId(projectId);
    }

    public List<BrandExposureSnapshot> page(Long projectId, int pageNum, int pageSize) {
        int offset = (pageNum - 1) * pageSize;
        return snapshotMapper.selectByPage(projectId, offset, pageSize);
    }

    public int count(Long projectId) {
        return snapshotMapper.countByProjectId(projectId);
    }
}


